﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FreeDream.Common
{
    /// <summary>
    /// DES加密/解密类。
    /// </summary>
    public class DEncrypt
    {
        public static string BASE_Key = "c0f4a97b-20e8-4787-b09b-91eb9eba9981"; //此密钥不可更改
        public static System.Text.Encoding encoding = System.Text.Encoding.UTF8;//编码类型不可更改
        public static byte[] Decrypt(byte[] encrypted)
        {
            byte[] buffer1 = encoding.GetBytes(BASE_Key);
            return DEncrypt.Decrypt(encrypted, buffer1);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="original"></param>
        /// <returns></returns>
        public static string Decrypt(string original)
        {
            return DEncrypt.Decrypt(original, BASE_Key, encoding);
        }

        public static byte[] Decrypt(byte[] encrypted, byte[] key)
        {
            TripleDESCryptoServiceProvider provider1 = new TripleDESCryptoServiceProvider();
            provider1.Key = DEncrypt.MakeMD5(key);
            provider1.Mode = CipherMode.ECB;
            return provider1.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
        }

        public static string Decrypt(string original, string key)
        {
            return DEncrypt.Decrypt(original, key, encoding);
        }

        public static string Decrypt(string encrypted, string key, Encoding encoding)
        {
            byte[] buffer1 = Convert.FromBase64String(encrypted);
            byte[] buffer2 = encoding.GetBytes(key);
            return encoding.GetString(DEncrypt.Decrypt(buffer1, buffer2));
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="original"></param>
        /// <returns></returns>
        public static string Encrypt(string original)
        {
            return DEncrypt.Encrypt(original, BASE_Key);
        }

        public static byte[] Encrypt(byte[] original)
        {
            byte[] buffer1 = encoding.GetBytes(BASE_Key);
            return DEncrypt.Encrypt(original, buffer1);
        }

        public static string Encrypt(string original, string key)
        {
            byte[] buffer1 = encoding.GetBytes(original);
            byte[] buffer2 = encoding.GetBytes(key);
            return Convert.ToBase64String(DEncrypt.Encrypt(buffer1, buffer2));
        }

        public static byte[] Encrypt(byte[] original, byte[] key)
        {
            TripleDESCryptoServiceProvider provider1 = new TripleDESCryptoServiceProvider();
            provider1.Key = DEncrypt.MakeMD5(key);
            provider1.Mode = CipherMode.ECB;
            return provider1.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
        }

        public static byte[] MakeMD5(byte[] original)
        {
            byte[] buffer1 = new MD5CryptoServiceProvider().ComputeHash(original);
            return buffer1;
        }


        public static string MakeMD5(string strSource)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            //获取密文字节数组
            byte[] bytResult = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(strSource));
            //转换成字符串，32位
            string strResult = BitConverter.ToString(bytResult);
            //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符，需要去除掉 
            strResult = strResult.Replace("-", "");
            return strResult;
        }
    }

    public class Encrypt
    {
        private const string CIV = "CBxs7X2+kgF=";//密钥
        private const string CKEY = "FwIXHSKgUCD=";//初始化向量

        public static string EncryptString(string Value)
        {
            SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;

            ct = mCSP.CreateEncryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV));

            byt = Encoding.UTF8.GetBytes(Value);

            ms = new MemoryStream();
            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
            cs.Write(byt, 0, byt.Length);
            cs.FlushFinalBlock();

            cs.Close();

            return Convert.ToBase64String(ms.ToArray());
        }

        public static string DecryptString(string Value)
        {
            SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;

            try
            {
                ct = mCSP.CreateDecryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV));


                byt = Convert.FromBase64String(Value);

                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();

                cs.Close();

                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
